package ar.Location.Helper;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import android.util.Log;

public class HttpProvider {
	private static final String TAG = "HttpProvider";

	public static String getHttpContent(String websiteUrl) {
		String page = null;
		HttpURLConnection urlConnection = null;
		try {
			URL url = new URL(websiteUrl);
			urlConnection = (HttpURLConnection) url.openConnection();
			int code = urlConnection.getResponseCode();
			Log.d(TAG, "Code response: " + code);
			InputStream in = new BufferedInputStream(
					urlConnection.getInputStream());
			page = new java.util.Scanner(in).useDelimiter("\\A").next();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (urlConnection != null) {
				urlConnection.disconnect();
			}
		}
		return page;
	}

	public static InputStream getInputStream(String websiteUrl) {

		InputStream is = null;
		try {
			URL url = new URL(websiteUrl);
			HttpGet httpRequest = null;

			httpRequest = new HttpGet(url.toURI());

			HttpClient httpclient = new DefaultHttpClient();
			HttpResponse response = (HttpResponse) httpclient
					.execute(httpRequest);

			HttpEntity entity = response.getEntity();
			BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
			is = bufHttpEntity.getContent();

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return is;

	}
}
